{{!
  Copyright (c) Meta Platforms, Inc. and affiliates.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

}}{{!

    NOTE:
    Unfortunately map literals cannot be used to store metadata, due to a bug
    in the Go compiler: https://github.com/golang/go/issues/33437
    The bug results in a "NewBulk too big" compilation error for some very large schemas.
    The workaround is to create slice literals (one with keys, one with values),
    create an empty map during runtime and populate that map from the two slices.

}}
{{#import "common/constants" as constants}}
{{> common/auto_generated_go}}

package {{program:go_pkg_name}}

import (
    {{#program:thrift_imports}}
    {{program:go_package_alias}} "{{program:go_import_path}}"
    {{/program:thrift_imports}}
    thrift "{{constants.thrift_lib_import}}"
)

// (needed to ensure safety because of naive import list construction)
{{> common/unused_imports_protection}}
var _ = thrift.VOID

// Premade codec specs
var (
    {{#program:thrift_metadata_types}}
    {{type:codec_type_spec_name}} = &thrift.TypeSpec{
        FullName: "{{type:full_name}}",
        {{> spec/codec_type_spec_setter}}:
            {{> spec/codec_type_spec_instance}}
    }
    {{/program:thrift_metadata_types}}
)

// Premade struct specs
var (
    {{#program:structs}}
    {{struct:struct_spec_name}} =
        {{> spec/codec_struct_spec}}
    {{/program:structs}}
    {{#program:req_resp_structs}}
    {{struct:struct_spec_name}} =
        {{> spec/codec_struct_spec}}
    {{/program:req_resp_structs}}
)

var premadeCodecSpecsMap = func() map[string]*thrift.TypeSpec {
    fbthriftTypeSpecsMap := make(map[string]*thrift.TypeSpec)
    {{#program:thrift_metadata_types}}
    {{#type:named?}}
    fbthriftTypeSpecsMap[{{type:codec_type_spec_name}}.FullName] = {{type:codec_type_spec_name}}
    {{/type:named?}}
    {{/program:thrift_metadata_types}}
    return fbthriftTypeSpecsMap
}()

// GetMetadataThriftType (INTERNAL USE ONLY).
// Returns metadata TypeSpec for a given full type name.
func GetCodecTypeSpec(fullName string) *thrift.TypeSpec {
    return premadeCodecSpecsMap[fullName]
}
